package se.energimyndigheten.lampguiden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.bulbreplacement.SocketActivity;
import se.energimyndigheten.lampguiden.conversiontable.ConversionActivity;
import se.energimyndigheten.lampguiden.info.InfoListActivity;
import se.energimyndigheten.lampguiden.lighting.LightingActivity;

/** Superclass that gives activity action bar with navigation control. */
@SuppressLint("AlwaysShowAction")
public abstract class SubPageActivity extends SherlockFragmentActivity implements ActionBar.OnNavigationListener {

    protected final static int ACTIVITY_CONVERSION = DashboardActivity.ACTIVITY_CONVERSION;
    protected final static int ACTIVITY_INFO = DashboardActivity.ACTIVITY_INFO;
    protected final static int ACTIVITY_LIGHTING = DashboardActivity.ACTIVITY_LIGHTING;
    protected final static int ACTIVITY_SOCKET = DashboardActivity.ACTIVITY_SOCKET;
    protected final static String HTML_BASEPATH = DashboardActivity.HTML_BASEPATH;

    protected int mSelectedActivity;
    protected boolean mShowHelpIcon;
    private String mHelpAssetFile;

    private final static String TAG = SubPageActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mShowHelpIcon = false;

        // create section list
        final Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
                context,
                R.array.sections,
                R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        ActionBar bar = getSupportActionBar();
        bar.setIcon(R.drawable.ic_actionbar);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        bar.setListNavigationCallbacks(list, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionBar bar = getSupportActionBar();
        bar.setSelectedNavigationItem(mSelectedActivity);
    }

    /*** Handle action bar app icon click events. Go to start activity (dashboard). */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.v(TAG, "onOptionsItemSelected");

        final int menuId = item.getItemId();
        Intent intent;
        switch (menuId) {
        case android.R.id.home:
            // home icon in action bar clicked; go home
            intent = new Intent(this, DashboardActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            break;
        case R.drawable.ic_fragetecken:
            intent = new Intent(this, HelpActivity.class);
            intent.putExtra(HelpActivity.EXTRA_HTML_PATH, mHelpAssetFile);
            intent.putExtra(HelpActivity.EXTRA_TITLE, getResources().getString(getMyTitle()));
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        if (intent != null) {
            startActivity(intent);
        }
        return true;
    }

    private int getMyTitle() {
    	switch(getSupportActionBar().getSelectedNavigationIndex()) {
    	case ACTIVITY_CONVERSION:
    		return R.string.section_conversion_title;
    	case ACTIVITY_INFO:
    		return R.string.section_info_title;
        case ACTIVITY_LIGHTING:
        	return R.string.section_lighting_title;
        case ACTIVITY_SOCKET:
        	return R.string.section_socket_title;
    	}
		return 0;
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (mShowHelpIcon) {
            menu.add(0, R.drawable.ic_fragetecken, 0, R.string.actionitem_help).setIcon(R.drawable.ic_fragetecken)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        Log.v(TAG, "onNavigationItemSelected :: pos: " + itemPosition + " id: " + itemId);

        if (mSelectedActivity == itemPosition) {
            return true;
        }

        Intent intent;

        switch (itemPosition) {
        case ACTIVITY_SOCKET:
            intent = new Intent(this, SocketActivity.class);
            break;
        case ACTIVITY_LIGHTING:
            intent = new Intent(this, LightingActivity.class);
            break;
        case ACTIVITY_CONVERSION:
            intent = new Intent(this, ConversionActivity.class);
            break;
        case ACTIVITY_INFO:
            intent = new Intent(this, InfoListActivity.class);
            break;
        default:
            intent = null;
        }
        if (intent != null) {
            startActivity(intent);
        }
        return true;
    }

    @SuppressLint("NewApi")
    protected void showHelpActionItem(String fileName) {
        mHelpAssetFile = fileName;
        mShowHelpIcon = true;
        invalidateOptionsMenu();
    }

    @SuppressLint("NewApi")
    protected void hideHelpActionItem() {
        mShowHelpIcon = false;
        invalidateOptionsMenu();
    }
}
